<?php
class Posts_Model_DbTable_Posts extends Zend_Db_Table_Abstract
{
    protected $_name = 'posts';
	
	/* lay danh sach posts */
	public function getPosts(){	
    	$row = $this->fetchAll();
    	if (!$row) {
    		throw new Exception("Could not find row");
    	}
    	return $row->toArray();
    }
	
	/* 
	- Lay posts theo id
	- Chuyen id vao
	*/
	public function getByPostView($id){	
    	$id = (int)$id;
    	$row = $this->fetchRow('id = ' . $id);
    	if (!$row) {
    		throw new Exception("Could not find row $id");
    	}
    	return $row->toArray();
    }
	
	/*TODO: cap nhat so lan xem bai viet*/
	public function updateByPostView($id){	
    	$id = (int)$id;    	
    }
	

	 public function addPost($post_author, $post_datetime, $post_content, $post_title,
					$post_status, $post_parent, $post_type, $post_view, $post_category)
    {
    	$data = array(
    			'post_author' => $post_author,
    			'post_datetime' => $post_datetime,
    			'post_content' => $post_content,
    			'post_title' => $post_title,
    			'post_status' => $post_status,
    			'post_parent' => $post_parent,
    			'post_type' => $post_type,
    			'post_view' => $post_view,	
				'post_category' => $post_category				
    	);
		
    	$this->insert($data);
    }
	
	public function updatePost($id, $post_author, $post_datetime, $post_content, $post_title,
					$post_status, $post_parent, $post_type, $post_view, $post_category)
    {
    	$data = array(
    			'post_author' => $post_author,
    			'post_datetime' => $post_datetime,
    			'post_content' => $post_content,
    			'post_title' => $post_title,
    			'post_status' => $post_status,
    			'post_parent' => $post_parent,
    			'post_type' => $post_type,
    			'post_view' => $post_view,	
				'post_category' => $post_category				
    	);

		echo "<pre>";
    	print_r ( $data);
    	echo "</pre>";
		
		$this->update($data, 'ID = '. (int)$id);
    }
	 public function deletePost($id)
    {
    	$this->delete('id =' . (int)$id);
		return 1;
    }
	
	 public function getListPosts($postuserid)
    {			
    	$row = $this->fetchall("post_author = $postuserid AND post_parent = 0 AND post_status=1");
    	if (!$row) {
    		throw new Exception("Could not find row ");
    	}
		
		$rowvalue = $row->toArray();
		
		/*
		echo "<pre>";
		print_r($rowvalue);
		echo "</pre>";
		exit;
		
			*/
	

	
		/* dag lam */
/*
				$db = Zend_Db_Table::getDefaultAdapter(); 
				$select = $db->select()
				 ->from(array('p' => 'posts'),
						array('post_datetime', 'post_title'))
				 ->join(array('u' => 'users'),
						'u.ID = p.post_author');
					*/
	$db = Zend_Db_Table::getDefaultAdapter(); 
			
											
		

		
//		exit;
				
							 
			/* end dang lam */
	
	
	
	
	
		$value=array();
		$value['row']= $rowvalue;
		foreach ($rowvalue AS $k => $row)
		{
			//$rowparent = $this->fetchall('post_status=1 AND post_parent ='.$row['ID']);			
		//	$value['rowparent'][$k] = $rowparent->toArray();
			
				$select = $db->select()
				 ->from(array('p' => 'posts'),
						array('ID','post_author AS user_id','post_datetime','post_content',
						'post_title'
						)
						)
						
				 ->join(array('u' => 'users'),
						'u.ID = p.post_author',
						array('user_name')
						)
				 ->where('p.post_parent=?',$row['ID'])
				  ->where('p.post_status=?',1);
						
				$value['rowparent'][$k]  = $db->fetchAll($select);
				/*
								
						echo "<pre>";
						print_r($rows);
						echo "</pre>";
					*/
		
		}
    	return $value;
		
    }
	
	public function getListPostsByUser($postuserid, $like_user)
    {		

		$db = Zend_Db_Table::getDefaultAdapter(); 
    	$row = $this->fetchall("post_author = $postuserid AND post_status=1");
    	if (!$row)
		{
    		throw new Exception("Could not find row ");
    	}
		$rowvalue = $row->toArray();
		
		$value=array();
		$value['row']= $rowvalue;
				
		foreach ($rowvalue AS $k => $row)
		{

				$str = "select ID from likes WHERE like_tables=1 AND like_tables_id=$row[ID] AND like_user=$like_user";
				$stmt = $db->query($str);
				$result = $stmt->fetch();
	
		
				$value['row'][$k]['like_status'] = $result['ID'];
				$value['row'][$k]['like'] = "Like";
				if($result['ID'] != NULL)
				{
					$value['row'][$k]['like'] = "UnLike";
				}
				
				
			
			
				$str = "SELECT u.`user_name` FROM likes AS l, users AS u WHERE l.`like_user` = u.`ID`
						AND	like_tables=1 AND like_tables_id=$row[ID] AND l.`like_user` != $like_user";
				$stmt = $db->query($str);
				$result = $stmt->fetchAll();
				$value['userlike'][$k] = $result;
				
				$select = $db->select()
				 ->from(array('c' => 'comments'),
						array('ID','comment_author AS user_id','comment_postdate','comment_content')
						
						)
				
				 ->join(array('u' => 'users'),
						'u.ID = c.comment_author',
						array('user_name')
						)
				 ->where('c.post_parent=?',$row['ID'])
				  ->where('c.comment_status=?',1);
					
				$value['rowparent'][$k]  = $db->fetchAll($select);
				
				foreach ($value['rowparent'][$k] AS $i=> $pr )
				{			
					$str = "select ID from likes WHERE like_tables=2 AND like_tables_id=$pr[ID] AND like_user=$like_user";
					$stmt = $db->query($str);
					$result = $stmt->fetch();
					$value['rowparent'][$k][$i]['like_status'] = $result['ID'];
					$value['rowparent'][$k][$i]['like'] = "Like";
					
					if($result['ID'] != NULL)
					{
						$value['rowparent'][$k][$i]['like'] = "UnLike";
					}
					
					$str = "SELECT u.`user_name` FROM likes AS l, users AS u WHERE l.`like_user` = u.`ID`
					AND	like_tables=2 AND like_tables_id=$pr[ID] AND l.`like_user` != $like_user";
					$stmt = $db->query($str);
					$result = $stmt->fetchAll();
					// $value['rowparent'][$k][$i]['userlike'] = $result;
					$value['rowparent'][$k][$i]['userlike'] = $result;
				}
				
		
			
		}
				
			
		
    	return $value;
	}
}

